﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    //TODO: test
    /// <summary>
    /// 
    /// </summary>
    public class PolarTransformation : BaseImageFilter
    {
        protected int m_nColumn = 10;
        protected int m_nRow = 10;
        protected int m_nResultWidth = 17;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="PolarTransformation"/> class.
        /// </summary>
        public PolarTransformation()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PolarTransformation"/> class.
        /// </summary>
        /// <param name="_polarTransofrmation">The _polar transofrmation.</param>
        internal PolarTransformation(PolarTransformation _polarTransofrmation)
        {

        }

        /// <summary>
        /// Gets or sets the width of the result.
        /// </summary>
        /// <value>The width of the result.</value>
        public int ResultWidth
        {
            get { return m_nResultWidth; }
            set { m_nResultWidth = value; }
        }

        /// <summary>
        /// Gets or sets the row.
        /// </summary>
        /// <value>The row.</value>
        public int Row
        {
            get { return m_nRow; }
            set { m_nRow = value; }
        }

        /// <summary>
        /// Gets or sets the column.
        /// </summary>
        /// <value>The column.</value>
        public int Column
        {
            get { return m_nColumn; }
            set { m_nColumn = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            int y = 0;
            int x = 0;

            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            Color color = Color.Transparent;
            int xr = 0;
            int yr = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmapCloned.GetPixel(x, y);
                    xr = (int)Math.Cos(2 * Math.PI * (x / m_nResultWidth)) * y + m_nColumn;
                    yr = (int)Math.Sin(2 * Math.PI * (x / m_nResultWidth)) * y + m_nRow;
                    _bitmap.SetPixel(xr, yr, color);
                }
            }
            bitmapCloned.Dispose();
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new PolarTransformation(this);
        }
    }
}
